package com.ry.video;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;

public class ImageToVideoConverter {

    public static void createVideoFromImages(String inputPattern, String outputVideo, int frameRate, int width, int height) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "ffmpeg",
                    "-framerate", String.valueOf(frameRate),
                    "-i", inputPattern,
                    "-c:v", "libx264",
                    "-pix_fmt", "yuv420p",
                    "-vf", String.format("scale=%d:%d", width, height),
                    outputVideo
            );

            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();

            int exitCode = process.waitFor();
            System.out.println("FFmpeg exited with code: " + exitCode);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        String inputPattern = "material/image1.jpg"; // 图片命名格式
        BufferedImage bufferedImage= ImageIO.read(new File(inputPattern));
        String outputVideo = "material/out/output.mp4";
        int frameRate = 30;
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();

        createVideoFromImages(inputPattern, outputVideo, frameRate, width, height);
    }
}

